package org.jeecg.modules.utils;

import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;

public class MacAddressUtil {

    /**
     * 获取本机的第一个有效 MAC 地址。
     *
     * @return 有效的 MAC 地址字符串（格式：00:A1:B2:C3:D4:E5），如果无法获取则返回 null。
     */
    public static String getMacAddress() {
        try {
            Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();
            while (networkInterfaces.hasMoreElements()) {
                NetworkInterface networkInterface = networkInterfaces.nextElement();

                // 过滤掉回环接口、未启用的接口和虚拟接口
                if (networkInterface.isLoopback() || !networkInterface.isUp() || networkInterface.isVirtual()) {
                    continue;
                }

                byte[] mac = networkInterface.getHardwareAddress();
                if (mac != null && mac.length > 0) {
                    StringBuilder macAddress = new StringBuilder();
                    for (int i = 0; i < mac.length; i++) {
                        macAddress.append(String.format("%02X%s", mac[i], (i < mac.length - 1) ? ":" : ""));
                    }
                    return macAddress.toString();
                }
            }
        } catch (SocketException e) {
            e.printStackTrace();
        }
        return null; // 如果没有找到有效的 MAC 地址，则返回 null
    }

    /**
     * 获取所有网络接口的 MAC 地址。
     *
     * @return 包含所有有效 MAC 地址的字符串数组。
     */
    public static String[] getAllMacAddresses() {
        try {
            Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();
            StringBuilder macAddresses = new StringBuilder();

            while (networkInterfaces.hasMoreElements()) {
                NetworkInterface networkInterface = networkInterfaces.nextElement();

                // 过滤掉回环接口、未启用的接口和虚拟接口
                if (networkInterface.isLoopback() || !networkInterface.isUp() || networkInterface.isVirtual()) {
                    continue;
                }

                byte[] mac = networkInterface.getHardwareAddress();
                if (mac != null && mac.length > 0) {
                    StringBuilder macAddress = new StringBuilder();
                    for (int i = 0; i < mac.length; i++) {
                        macAddress.append(String.format("%02X%s", mac[i], (i < mac.length - 1) ? ":" : ""));
                    }
                    macAddresses.append(macAddress.toString()).append("\n");
                }
            }

            return macAddresses.toString().split("\n");
        } catch (SocketException e) {
            e.printStackTrace();
        }
        return new String[0]; // 如果没有找到任何 MAC 地址，则返回空数组
    }

    public static void main(String[] args) {
        // 测试获取第一个有效 MAC 地址
        String macAddress = getMacAddress();
        System.out.println("First MAC Address: " + macAddress);

        // 测试获取所有 MAC 地址
        String[] allMacAddresses = getAllMacAddresses();
        System.out.println("All MAC Addresses:");
        for (String address : allMacAddresses) {
            System.out.println(address);
        }
    }
}